<?php 
include('../Connections/KRAPPERCON.php');
include ("../Connections/Accesos.php");
$oper = '';
?>

<div>
<input type="button" value="Nuevo" onClick="newNC();">&nbsp;
<input type="button" value="Grabar" onClick="grabarNC()">&nbsp;
<input type="text" id="t_nc_fc" onKeyPress="clickRefFac(event)" placeholder='Fact.Referencia'>
</div>

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tbl_nc_dat">
  <tr>
<td></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Clase Documento</td>
    <td>
      <select name="cbo_nc_cldoc" id="cbo_nc_cldoc" onchange="selTblNC(this)">
            <?php 
       $cladoc = 'FV';
	 	include ('../Query_Min/getClaDocsNC.php');
      ?>
      </select>
    </td>
    <td>Documento :</td>
    <td><a style="font-weight:bold" href="#" id="sp_new_nc" onClick="loadPageFact($(this).html())"></a></td>
  </tr>
  <tr>
    <td>Fecha Documento </td>
    <td>
      <input type="text" class="clsReq" name="t_nc_fdoc" id="t_nc_fdoc">
    </td>
    <td>Documento Previo:</td>
    <td><a style="font-weight:bold" href="#" id="sp_nc_nodoc" onClick="loadPageFact($(this).html())"></a></td>
  </tr>
  <tr>
    <td><strong>Asignacion N/C</strong></td>
    <td>
      <input style="font-weight:bold" class="disabled" type="text" name="t_nc_folio" id="t_nc_folio" disabled>    </td>
    <td>Referencia Doc.Previo:</td>
    <td><span id="sp_nc_noref"></span></td>
  </tr>
  <tr>
    <td>Cliente</td>
    <td colspan="3"><!-- <a id="sp_nc_ccli"></a>&nbsp;<span id="sp_nc_ncli"></span>-->
    <select id="cbo_nc_cli" name="cbo_nc_cli" onchange="GEBI('d_nc_cli').innerHTML = $(this).find(':selected').attr('title');">
    <?php
	$combo_cli = '';
	include('../Query_Min/getClientes.php');
	echo $combo_cli;
	?>
    </select>
    <div id="d_nc_cli" style="margin:5px;font-weight:bold;"></div></td>
  </tr>
  <tr>
    <td>Texto :</td>
    <td colspan="3">
      <textarea name="t_nc_text" class="clsReq" cols="60" id="t_nc_text"></textarea>
    </td>
  </tr>

  <tr>
    <td colspan="4">
    <div id="d_res_nc" style="margin:7px;"></div>    </td>
  </tr>
  <tr>
    <td>SubTotal :</td>
    <td><input name="t_nc_subt" class="qt disabled" disabled="disabled"  type="text" id="t_nc_subt" size="15" align="right" />    </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>IGV</td>
    <td><input name="t_nc_igv" class="qt disabled" disabled="disabled"  type="text" id="t_nc_igv" size="15"  align="right"/>    </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Total :</td>
    <td><input name="t_nc_total" class="qt disabled" disabled="disabled" type="text" id="t_nc_total" size="15" align="right" />    </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<script type="text/javascript">
function newNC(){
loadPage('Procesos/MantNC.php');
}

function grabarNC(){

if (!$("#tbl_nc_dat").checkInput()){
return;
}

var items = '';
var pfac =$('#sp_nc_nodoc').html();

if (pInt(pfac) == 0){
showError('Debe seleccionar un Documento de referencia');
return;
}
var cnt = 0;

var n = $('#d_res_nc tbody tr').each(function(){
	var fila = $(this);
	var chk = fila.find('.clsChkNC');
	if (chk.is(':checked')){
	var pos = pInt(fila.attr('title').substring(4));
	var qty = pInt(fila.find('.clsNcQty').valx());
	var val = pFloat(fila.find('.clsNcImp').val());
	items = items + pos + '|' + qty  + '|' +  val  + '#';
	cnt ++;
	}
});

if (cnt == 0){
	showError('No se permite Documento sin Items');
	return;
}
  
sendData('Procesos/Opers/OperNC.php',
	{ 
	oper : 'save', 
	factura : pfac, 
	fecha : GEBI('t_nc_fdoc').value, 
	clase : GEBI('cbo_nc_cldoc').value , 
	cliente : GEBI('cbo_nc_cli').value,
	texto : GEBI('t_nc_text').value,
	items : items
	}
	);
}

function loadfromFac(numfac){
GEBI('sp_new_nc').innerHTML = '';
$("#d_res_nc").loadx('Procesos/Opers/OperNC.php',{ oper : 'load' , factura : numfac});
}

function clickRefFac(e){
if (e.keyCode == 13){	
	loadfromFac(GEBI('t_nc_fc').value);
	GEBI('t_nc_fc').value = '';
}
}

function selTblNC(cbo){
	var tipo = cbo.value;
	var numfac = GEBI('sp_nc_nodoc').innerHTML;
//	if (tipo == 'FC_N3'){
	// Permitir ingresar valores 
	if (pFloat(numfac)>0){
	$("#d_res_nc").loadx('Procesos/Opers/OperNC.php',{ oper : 'reload' , factura : numfac, clase : tipo});
	}
	//}
}

function calcNetNC(obj){
	//alert('');
	
		var vIgv = 0;
		var vNet = 0;
		var vImp = 0;
		var xigv = 0;
		var xmon = 0;
		var ximp = 0;
	
	var isVal = pInt($("#cbo_nc_cldoc option:selected").attr('val'));
	//alert(isVal);	
	$('.clsChkNC').each(function(){
		if (this.checked){
		//alert('');
		var fila = $(this).parent().parent();
		if (isVal == 0){
			var tg_mon = fila.find('.clsNcImp');
			var mon = 0;
			//alert(tg_mon.html());
			if (tg_mon.prop('tagName') == 'INPUT'){
			mon = pFloat(tg_mon.val());
			}
			else{
			mon = pFloat(tg_mon.html());
			}
		//alert(mon);
		}
		else {
		
			var pru = pFloat(fila.find(".clsNcPru").valx());
			var pvu = pFloat(fila.find(".clsNcPvu").valx());
			var ctd = pFloat(fila.find(".clsNcQty").valx());
			// var dct = pFloat(fila.find('.clsNcPdc').html());
			
			//var mon = pFloat(myRound(( pru * ( 1- ( dct * 0.01))) * ctd ));
			/*
			ximp = pFloat(myRound(pvu * ( 1 - (dct * 0.01)) * ctd));
			xigv = pFloat(myRound(ximp / (1 + igv)));
			xmon = ximp - xigv;
			*/
			ximp = pFloat(myRound(pvu  * ctd));
			xigv = pFloat(myRound(ximp / (1 + igv)));
			xmon = ximp - xigv;
			//vImp =  mon;
//		alert(mon);
		
		}
		//var vigv = myRound(mon * igv);
		fila.find('.clsNcIva').html(xigv);
		fila.find('.clsNcImp').html(myRound(xmon));
		//alert(vImp);
		vImp = pFloat(vImp) + pFloat(ximp);
		//vIgv = pFloat(vIgv) + pFloat(xigv);
		/*
		var net = $('.clsNcIva').each(function(){
		 var row = $(this).parent();
		 
		 vIgv = vIgv + pFloat(row.find('.clsNcIva').html());
		 vImp = vImp + pFloat(row.find('.clsNcImp').val());		 
		// vNet = vNet + pFloat(row.find('.clsNcNet').html());		 
		});
		*/
		}
	});
	
	GEBI('t_nc_total').value = myRound(vImp);
	vNet = myRound(vImp / (1+igv));
	GEBI('t_nc_subt').value = myRound(vNet);
	vIgv = vImp - vNet;
	GEBI('t_nc_igv').value = myRound(vIgv);
	//vNet = pFloat(vIgv) + pFloat(vImp);
	//alert(vNet);
	
}

</script>
